/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.surveilans.controller;

import com.surveilans.classes.Kabupaten;
import com.surveilans.classes.Penyakit;
import com.surveilans.classes.SMS;
import com.surveilans.model.*;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 * @author Master
 */
public class DetectController implements ServletContextListener {

    DetectModel detect;
    DataModel data;
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        detect = new DetectModel();
        data = new DataModel();
        ServletContext servletContext = sce.getServletContext();
        try {
            // create the timer and timer task objects
            Timer timer = new Timer();
            CheckKLB task = new CheckKLB(); //this class implements Callable.

            // get a calendar to initialize the start time
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 6);
            calendar.add(Calendar.DATE, 1);
            Date startTime = calendar.getTime();
            
            timer.scheduleAtFixedRate(task, startTime, 1000 * 60 * 10);

            // save our timer for later use
            servletContext.setAttribute("timer", timer);
        } catch (Exception e) {
            servletContext.log("Problem initializing the task that was to run hourly: " + e.getMessage());
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        
    }
    
    public class CheckKLB extends TimerTask {

        @Override
        public void run() {
            ArrayList<Kabupaten> ak = data.getKabupaten();
            ArrayList<Penyakit> ap = data.getPenyakit();
            Iterator itk = ak.iterator();
            Iterator itp = ap.iterator();            
            while (itk.hasNext()) {
                Kabupaten k = (Kabupaten) itk.next();
                while (itp.hasNext()) {
                    Penyakit p = (Penyakit) itp.next();
                    System.out.println(k.getNama()+":"+p.getNama());
                    //detect.checkKLB(k.getId(), p.getId());
                }
                itp = ap.iterator();
            }
        }
    }
    
}
